"""
    现有 1000元
    哥哥： 积木 200
    妹妹： 大熊猫玩偶  100
"""
# money = 1000
#
# def child_buy(money,toy,price):
#     money -= price
#     print(f"买了{toy},花费了{price},还剩{money}元")
#
# child_buy(money,"积木",200)
# child_buy(money,"大熊猫玩偶",100)


# 闭包实现
def get_money(money):

    print(f"我有{money}元")
    def child_buy(toy,price):
        # global money
        # nonlocal 关键字允许嵌套函数中修改外部全局变量
        nonlocal money
        money -= price
        print(f"买了{toy},花费了{price},还剩{money}元")
    return child_buy

# money = 1000
child_buy = get_money(1000)
child_buy("积木",200)
child_buy("大熊猫玩偶",100)
